package com.smart.buss.ctrl;

import com.smart.buss.service.BussAccountLogService;
import com.smart.buss.service.BussUserService;
import com.smart.buss.vo.BussAccountLog;
import com.smart.buss.vo.BussUser;
import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.common.Constants;
import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.common.util.PropertiesUtil;
import com.smart.primary.common.util.Util;
import com.smart.primary.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.WebUtils;

import java.util.*;

@RestController
@RequestMapping("/buss/account/log")
public class BussAccountLogController extends BaseCtrl {

	@Autowired
	private BussAccountLogService bussAccountLogService;
	@Autowired
	private BussUserService bussUserService;

	@RequestMapping
	public AjaxResult getList(BussAccountLog entity) throws Exception{
		List<BussAccountLog> list=bussAccountLogService.queryListByPage(entity);
		return getResult(entity, list);
	}
//http://localhost:8080/task/buss/account/log/list?userId=12345678910
	@RequestMapping("/list")
	public AjaxResult list(BussAccountLog entity) throws Exception{
		String phone = getUserMobile();
		if(StringUtils.isNotBlank(phone)){
			entity.setUserId(phone);
		}
		if(StringUtils.isBlank(entity.getUserId())){
			return AjaxResult.success(new ArrayList<>());
		}
		entity.setField("create_date");
		entity.setOrder("DESC");
		List<BussAccountLog> list=bussAccountLogService.queryListByPage(entity);
		String path= PropertiesUtil.getString("res.web.url");
		for (BussAccountLog log: list) {
			if(StringUtils.isNotBlank(log.getImgIcon())){
				log.setImgIcon(path+log.getImgIcon());
			}
		}
		return AjaxResult.success(list);
	}

	@RequestMapping("/save")
	public AjaxResult save(BussAccountLog entity) throws Exception{
		if(StringUtils.isBlank(entity.getId())){
			entity.setId(Util.getUUID());
			bussAccountLogService.insert(entity);
			return AjaxResult.updateSuccess();
		}else {
			bussAccountLogService.update(entity);
			return AjaxResult.saveSuccess();
		}
	}

	/**
	 * 获取收益  徒弟带来的收益
	 */
	@RequestMapping("/tudi")
	public AjaxResult tudi(BussAccountLog entity) throws Exception{
		String phone = getUserMobile();
		if(StringUtils.isNotBlank(phone)){
			entity.setUserId(phone);
		}
		if(StringUtils.isBlank(entity.getUserId())){
			return AjaxResult.failure("未登录");
		}
		entity.setLogtype(3);
		List<BussAccountLog>  list=bussAccountLogService.queryList(entity);
		Double allprice=0.0;
		Double toprice=0.0;
		for (BussAccountLog log: list) {
			if(log.getCreateDate().getTime()> DateUtil.getStartDate(new Date()).getTime()){
				toprice+=log.getPrice();
			}
			allprice+=log.getPrice();
		}
		BussUser user =new BussUser();
		user.setFid(entity.getUserId());
		Integer tudinum=bussUserService.queryCount(user);
		Map m=new HashMap();
		m.put("toprice",toprice);
		m.put("allprice",allprice);
		m.put("tudinum",tudinum);
		return AjaxResult.success(m);
	}

	@RequestMapping("/del")
	public AjaxResult del(String id) throws Exception{
		bussAccountLogService.delete(id);
		return AjaxResult.deleteSuccess();
	}
}
